package com.schoolegard.services.impl;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.schoolegard.services.AlarmHandleService;
import com.schoolgard.dao.AlarmClassDao;
import com.schoolgard.dao.AlarmHandleDao;
import com.schoolgard.dao.RoleDao;
import com.schoolgard.entitybean.AlarmClass;
import com.schoolgard.entitybean.AlarmHandle;
import com.schoolgard.entitybean.Role;

public class AlaramHandleServiceImpl implements AlarmHandleService {
	private static Logger logger = Logger
			.getLogger(AlarmClassServiceImpl.class);
	private AlarmHandleDao alarmHandleDao;
	
	private AlarmClassDao alarmClassDao;
	
	private RoleDao roleDao;

	public int saveOrUpdate(AlarmHandle alarmHandle) {
		try {
			AlarmHandle handle = alarmHandleDao.createOrModify(alarmHandle);
			return handle == null ? 0 : handle.getId().intValue();
		} catch (Exception e) {
			logger.error("操作失败", e);
			return 0;
		}
	}

	public int delete(Long id) {
		try {
			alarmHandleDao.remove(findById(id));
			return 1;
		} catch (Exception e) {
			logger.error("操作失败", e);
			return 0;
		}
	}

	public AlarmHandle findById(Long id) {
		if(id == null || id == 0) {
			return null;
		}
		AlarmHandle handle = alarmHandleDao.findById(id);
		return handle;
	}

	public List<AlarmHandle> findAll(int firstResult,int maxResult) {
		List<AlarmHandle> list = alarmHandleDao.findAll(firstResult, maxResult);
		if(list == null || list.size() == 0) {
			return new ArrayList<AlarmHandle>(0);
		}
		return list;
	}

	public AlarmHandle findByClassId(Long alarmClassId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(AlarmHandle.class);
		criteria.add(Restrictions.eq("alarmClassId", alarmClassId));
		List<AlarmHandle> alarmHandles = alarmHandleDao.findByCriteria(criteria);
		if(alarmHandles == null || alarmHandles.size() == 0) {
			return null;
		} else {
			return alarmHandles.get(0);
		}
	}
	
	private String getAlarmClassName(Long alarmClassId){
		AlarmClass alarmClass= alarmClassDao.findById(alarmClassId);
		if(alarmClass != null) {
			return alarmClass.getName();
		}
		return "";
	}
	
	private String getRoleNames(String roleId){
		String[] roleIds = roleId.split(",");
		List<Role> roles = roleDao.findByIds(roleIds);
		if(roles == null || roles.size() == 0){
			return "";
		}
		StringWriter roleNames = new StringWriter();
		for(Role role : roles) {
			roleNames.append(role.getName());
		}
		return roleNames.toString();
	}
	
	public void setAlarmHandleDao(AlarmHandleDao alarmHandleDao) {
		this.alarmHandleDao = alarmHandleDao;
	}

	public AlarmHandleDao getAlarmHandleDao() {
		return alarmHandleDao;
	}

	public void setAlarmClassDao(AlarmClassDao alarmClassDao) {
		this.alarmClassDao = alarmClassDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

}
